/**
 * Copyright 2009-2015 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.ibatis.scripting.xmltags;

import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.session.Configuration;

/**
 * @author Eduardo Macarron XMLLanguageDriver为XML语言驱动，为MyBatis提供了通过XML标签（我们常用的<if>、<where>等标签）结合OGNL表达式语法实现动态SQL的功能
 */
public class XMLLanguageDriver implements LanguageDriver {

	@Override
	public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
		return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
	}

	@Override
	public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
		// 该方法用于解析XML文件中配置的SQL信息
		// 创建XMLScriptBuilder对象
		XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
		// 调用 XMLScriptBuilder对象parseScriptNode（）方法解析SQL资源
		return builder.parseScriptNode();
	}

	@Override
	public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
		// 该方法用于解析Java注解中配置的SQL信息
		// 字符串以<script>标签开头，则以XML方式解析
		if (script.startsWith("<script>")) {
			XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
			return createSqlSource(configuration, parser.evalNode("/script"), parameterType);
		} else {
			// 解析SQL配置中的全局变量
			script = PropertyParser.parse(script, configuration.getVariables());
			TextSqlNode textSqlNode = new TextSqlNode(script);
			// 如果SQL中是否仍包含${}参数占位符，则返回DynamicSqlSource实例，否则返回RawSqlSource
			if (textSqlNode.isDynamic()) {
				return new DynamicSqlSource(configuration, textSqlNode);
			} else {
				return new RawSqlSource(configuration, script, parameterType);
			}
		}
	}

}
